Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Typed::Result sealed #58

Merged
merged 2 commits into from
Jan 29, 2024
Merged

Make Typed::Result sealed #58

merged 2 commits into from
Jan 29, 2024

Conversation

maxveldink
Copy link
Owner

@maxveldink maxveldink commented Jan 27, 2024

Sorbet has the ability to mark a module/class as sealed, limiting inheritors to only the file where the abstract class was defined. Since we only want Typed::Success and Typed::Failure children, mark Result as sealed. This is technically a breaking change, as downstream code could have inherited from Result before this commit.

This also aids in flow typing. The newly added case type check test will fail on main right now because Sorbet can't exhaustively say all subtypes of Result were handled. With this change, we're able to demonstrate that the absurd case can never be ran.

Finally, since we no longer need to autoload success.rb and failure.rb, I will remove Zeitwerk in favor of manual requires for now.

We only ever want there to be two inheritors of `Typed::Result`, `Success` and `Failure`. We can express this in Sorbet with the `sealed!` helper. In order to use this, we must move the definitions of `Success` and `Failure` into the same file as `Result`. See https://sorbet.org/docs/sealed for more info.
@maxveldink
Copy link
Owner Author

@iMacTia Do you see any issues with moving Result to a sealed class?

@iMacTia
Copy link
Contributor

iMacTia commented Jan 28, 2024

Not a huge fan of the "everything in one file" approach, but the advantages are interesting and probably worth it!
Agree this is a breaking change, although I can't see a reason for adding a new subclass 🤔

If we really want to be thorough, we could release this as v2.0, but maybe that's a bit of a stretch.

@iMacTia
Copy link
Contributor

iMacTia commented Jan 28, 2024

Happy to also run some sanity checks against our monolith using this branch or main after merging, just to make sure everything works as expected in a real world scenario

@maxveldink
Copy link
Owner Author

| Not a huge fan of the "everything in one file" approach

Agreed, it was pretty weird to recombine everything. But I think the extra information we can give Sorbet about exhaustiveness is worth it.

I'll go ahead and merge this in and test our primary repos off of main as well. I'll wait til #56 and #57 are done to cut a new 2.0 but I should have time to do that this week.

@maxveldink maxveldink merged commit 0955290 into main Jan 29, 2024
7 checks passed
@maxveldink maxveldink deleted the maxveldink/sealed-class branch January 29, 2024 10:27
@iMacTia
Copy link
Contributor

iMacTia commented Feb 3, 2024

Finally got around to checkout main in our project and I can confirm everything works 🙌 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants